/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wwdablu.soumya.lottiebottomnavsample.slice;

import com.wwdablu.soumya.lottiebottomnav.FontBuilder;
import com.wwdablu.soumya.lottiebottomnav.FontItem;
import com.wwdablu.soumya.lottiebottomnav.ILottieBottomNavCallback;
import com.wwdablu.soumya.lottiebottomnav.LottieBottomNav;
import com.wwdablu.soumya.lottiebottomnav.MenuItem;
import com.wwdablu.soumya.lottiebottomnav.MenuItemBuilder;
import com.wwdablu.soumya.lottiebottomnav.util.LogUtil;
import com.wwdablu.soumya.lottiebottomnavsample.ResourceTable;
import com.wwdablu.soumya.lottiebottomnavsample.fraction.GiftFraction;
import com.wwdablu.soumya.lottiebottomnavsample.fraction.HomeFraction;
import com.wwdablu.soumya.lottiebottomnavsample.fraction.MailFraction;
import com.wwdablu.soumya.lottiebottomnavsample.fraction.SettingsFraction;
import com.wwdablu.soumya.lottiebottomnavsample.util.FontUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Color;

import java.util.ArrayList;

/**
 * MainAbilitySlice
 *
 * @since 2021-06-11
 */
public class MainAbilitySlice extends AbilitySlice implements ILottieBottomNavCallback, MailFraction.ClickHandler {
    private static final int MCURRENTPOSITION = -1;
    private static final int MSELECTEDTEXTSIZE = 16;
    private static final int MUNSELECTEDTEXTSIZE = 12;
    private static final int MINDEX = 2;
    private static final int MINDEX3 = 3;
    private static final String MESSAGEFILE = "message.json";
    private static final String CUPID = "cupid";
    private static final float MPAUSEDPROGRESS = 0.75f;
    private LottieBottomNav bottomNav;
    private StackLayout fractionLayout;
    private ArrayList<MenuItem> list;
    private HomeFraction homeFraction;
    private GiftFraction giftFraction;
    private MailFraction mailFraction;
    private SettingsFraction settingsFraction;

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(Color.getIntColor("#00574B"));
        super.setUIContent(ResourceTable.Layout_ability_main);
        bottomNav = (LottieBottomNav) findComponentById(ResourceTable.Id_bottom_nav);
        fractionLayout = (StackLayout) findComponentById(ResourceTable.Id_fractionlayout);
        list = new ArrayList<>();
        FontItem fontItem = FontBuilder.create("Dashboard")
                .selectedTextColor(Color.BLACK.getValue())
                .unSelectedTextColor(Color.GRAY.getValue())
                .selectedTextSize(MSELECTEDTEXTSIZE)
                .unSelectedTextSize(MUNSELECTEDTEXTSIZE)
                .setTypeface(FontUtil.getReadFont(this))
                .build();

        MenuItem item1 = MenuItemBuilder.create("home.json",
                fontItem, "dash")
                .pausedProgress(1f)
                .loop(false)
                .build();
        list.add(item1);

        fontItem = FontBuilder.create(fontItem).setTitle("Gifts").build();
        MenuItem item2 = MenuItemBuilder.createFrom(item1, fontItem)
                .selectedLottieName("gift.json")
                .unSelectedLottieName("gift.json")
                .loop(true)
                .build();
        list.add(item2);

        fontItem = FontBuilder.create(fontItem).setTitle("Mail").build();
        MenuItem item3 = MenuItemBuilder.createFrom(item1, fontItem)
                .selectedLottieName(MESSAGEFILE)
                .unSelectedLottieName(MESSAGEFILE)
                .pausedProgress(MPAUSEDPROGRESS)
                .build();
        list.add(item3);

        fontItem = FontBuilder.create(fontItem).setTitle("Settings").build();
        MenuItem item4 = MenuItemBuilder.createFrom(item1, fontItem)
                .selectedLottieName("settings.json")
                .unSelectedLottieName("settings.json")
                .build();
        list.add(item4);
        bottomNav.setCallback(this);
        bottomNav.setMenuItemList(list);

        bottomNav.setSelectedIndex(1);

        int count = bottomNav.getChildCount();
        LogUtil.error("geshi", count + "");
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * onMenuSelected
     *
     * @param oldIndex oldIndex
     * @param newIndex newIndex
     * @param menuItem menuItem
     */
    @Override
    public void onMenuSelected(int oldIndex, int newIndex, MenuItem menuItem) {
        selectFraction(newIndex);
    }

    /**
     * onAnimationStart
     *
     * @param index index
     * @param menuItem menuItem
     */
    @Override
    public void onAnimationStart(int index, MenuItem menuItem) {
        LogUtil.error("onAnimationStart", index + "");
    }

    /**
     * onAnimationEnd
     *
     * @param index index
     * @param menuItem menuItem
     */
    @Override
    public void onAnimationEnd(int index, MenuItem menuItem) {
        if (index == MINDEX && menuItem.getTag() != null && CUPID.equalsIgnoreCase(menuItem.getTag().toString())) {
            restoreMenuForMessage(menuItem);
        }
    }

    /**
     * onAnimationCancel
     *
     * @param index index
     * @param menuItem menuItem
     */
    @Override
    public void onAnimationCancel(int index, MenuItem menuItem) {
        if (index == MINDEX && menuItem.getTag() != null && CUPID.equalsIgnoreCase(menuItem.getTag().toString())) {
            restoreMenuForMessage(menuItem);
        }
    }

    /**
     * onChangeMenuIcon
     */
    @Override
    public void onChangeMenuIcon() {
        if (bottomNav.getSelectedIndex() == MINDEX) {
            MenuItem cupidMessage = MenuItemBuilder.createFrom(bottomNav.getMenuItemFor(MINDEX))
                    .selectedLottieName("message_cupid.json")
                    .tag(CUPID)
                    .build();

            bottomNav.updateMenuItemFor(MINDEX, cupidMessage);
        }
    }

    /**
     * getFractionManager
     *
     * @return FractionManager
     */
    private FractionManager getFractionManager() {
        FractionManager fractionManager = null;
        Ability ability = getAbility();
        if (ability instanceof FractionAbility) {
            FractionAbility fractionAbility = (FractionAbility) ability;
            fractionManager = fractionAbility.getFractionManager();
            return fractionManager;
        }
        return fractionManager;
    }

    /**
     * selectFraction
     *
     * @param index index
     */
    private void selectFraction(int index) {
        if (index == MCURRENTPOSITION) {
            return;
        }

        FractionScheduler fractionScheduler = getFractionManager().startFractionScheduler();
        hideAllFraction(fractionScheduler);

        switch (index) {
            case 0:
                if (homeFraction == null) {
                    homeFraction = new HomeFraction();
                    fractionScheduler.add(fractionLayout.getId(), homeFraction);
                } else {
                    fractionScheduler.show(homeFraction);
                }
                break;
            case 1:
                if (giftFraction == null) {
                    giftFraction = new GiftFraction();
                    fractionScheduler.add(fractionLayout.getId(), giftFraction);
                } else {
                    fractionScheduler.show(giftFraction);
                }
                break;
            case MINDEX:
                if (mailFraction == null) {
                    mailFraction = new MailFraction();
                    mailFraction.setClickHandler(this);
                    fractionScheduler.add(fractionLayout.getId(), mailFraction);
                } else {
                    fractionScheduler.show(mailFraction);
                }
                break;
            case MINDEX3:
                if (settingsFraction == null) {
                    settingsFraction = new SettingsFraction();
                    fractionScheduler.add(fractionLayout.getId(), settingsFraction);
                } else {
                    fractionScheduler.show(settingsFraction);
                }
                break;
            default:
                break;
        }
        fractionScheduler.submit();
    }

    /**
     * hideAllFraction
     *
     * @param fractionScheduler fractionScheduler
     */
    private void hideAllFraction(FractionScheduler fractionScheduler) {
        if (homeFraction != null) {
            fractionScheduler.hide(homeFraction);
        }
        if (giftFraction != null) {
            fractionScheduler.hide(giftFraction);
        }
        if (mailFraction != null) {
            fractionScheduler.hide(mailFraction);
        }
        if (settingsFraction != null) {
            fractionScheduler.hide(settingsFraction);
        }
    }

    /**
     * restoreMenuForMessage
     *
     * @param menuItem menuItem
     */
    private void restoreMenuForMessage(MenuItem menuItem) {
        MenuItem item = MenuItemBuilder.createFrom(menuItem)
                .selectedLottieName(MESSAGEFILE)
                .build();

        bottomNav.updateMenuItemFor(MINDEX, item);
    }
}
